package view;

import java.util.LinkedList;

import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;

import control.OrderControl;

import adamsLibrary.JComboBoxAuto;
import adamsLibrary.JComboBoxAutoLinkedList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

public class BlockPanel extends JPanel {
	private JComboBoxAuto comboBox;
	private OrderControl oc;
	private JComboBox comboBox_1;
	public BlockPanel(OrderControl oc) {
		setBackground(Color.BLACK);
		this.oc = oc;
		
		comboBox = new JComboBoxAuto(new GetBarcode());
		
		JButton btnBlock = new JButton("Block");
		btnBlock.addActionListener(new BlockPalletListener());
		
		JButton btnUnblock = new JButton("Unblock");
		btnUnblock.addActionListener(new UnblockPalletListener());
		
		JLabel lblBarcode = new JLabel("Barcode");
		lblBarcode.setForeground(Color.GREEN);
		
		String[] days = {"0","1","2","3","4","5","6","7","8","9","10"};
		
		JLabel lblDaysToBlock = new JLabel("Days to block");
		lblDaysToBlock.setForeground(Color.GREEN);
		
		comboBox_1 = new JComboBox(days);
		comboBox_1.setEditable(false);
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(lblBarcode, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 175, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
							.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
								.addContainerGap()
								.addComponent(lblDaysToBlock)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, 179, GroupLayout.PREFERRED_SIZE))
							.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
								.addGap(68)
								.addComponent(btnBlock)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(btnUnblock))))
					.addGap(272))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblBarcode)
						.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblDaysToBlock)
						.addComponent(comboBox_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnBlock)
						.addComponent(btnUnblock))
					.addContainerGap(198, Short.MAX_VALUE))
		);
		setLayout(groupLayout);
	}
	
	private class BlockPalletListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			oc.blockPallet(comboBox.getSelectedText(), true, comboBox_1.getSelectedItem());			
		}
		
	}
	private class UnblockPalletListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			oc.blockPallet(comboBox.getSelectedText(), false, comboBox_1.getSelectedItem());			
		}
		
	}
	
	private class GetBarcode implements JComboBoxAutoLinkedList{	
		
		@Override
		public LinkedList<String> getList(String string) {
			return oc.getBarcodes(string);
		}		
	}
}
